from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time


class login_about():

    def login(self, driver, username, password):
        a = driver.find_element(By.ID, 'username')
        a.send_keys(username)
        b = driver.find_element(By.XPATH, "//input[@id='password']")
        b.send_keys(password)
        c = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        c.click()
        time.sleep(1)

    def getres_login(self, driver):
        res = driver.title
        return res

    def getres_login1(self, driver):
        res = driver.find_element(By.XPATH, "//span[@class='help-block'][1]").text
        return res

    def review_login(self, res, expect):
        assert res == expect


class register_about():

    def open_register(self, driver):
        a = driver.find_element(By.PARTIAL_LINK_TEXT, "注册")
        a.click()
        time.sleep(1)

    def send_register(self, driver, name, pwd, repwd):
        a = driver.find_element(By.XPATH, "//input[@id='username']")
        a.send_keys(name)
        b = driver.find_element(By.XPATH, "//input[@id='password']")
        b.send_keys(pwd)
        c = driver.find_element(By.XPATH, "//input[@id='repassword']")
        c.send_keys(repwd)
        d = driver.find_element(By.XPATH, "//button[@id='login_btn']")
        d.click()
        time.sleep(1)

    def getres_register(self, driver):
        res = driver.title
        return res

    def getres_register1(self, driver):
        res = driver.find_element(By.XPATH, "//span[@class='help-block'][1]").text
        return res

    def review_register(self, res, expect):
        assert res == expect


class card_about():

    def find_cardadd(self, driver):
        el = WebDriverWait(driver, 5, 1).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "个人中心")))
        el.click()
        el1 = WebDriverWait(driver, 5, 1).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "银行卡管理")))
        el1.click()
        el2 = WebDriverWait(driver, 5, 1).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='bankCard_add_modal_btn']")))
        el2.click()

    def cardadd(self, driver, cardbank, leixing, cardnum):

        el = WebDriverWait(driver, 5, 2).until(EC.visibility_of_element_located((By.XPATH, "//input[@id='cardBank_add_input']")))
        el.send_keys(cardbank)

        if leixing == "信用卡":
            b = driver.find_element(By.XPATH, '//span[text()="信用卡"]')
            b.click()

        c = driver.find_element(By.XPATH, "//input[@id='cardNum_add_input']")
        c.send_keys(cardnum)

        d = driver.find_element(By.XPATH, "//button[@id='bankCard_save_btn']")
        d.click()
        time.sleep(2)

    def getres_card(self, driver):
        time.sleep(2)
        data = []
        a = driver.find_elements(By.XPATH, '//div[@class="col-sm-12"]//h4')[-3].text
        data.append(a)
        b = driver.find_elements(By.XPATH, '//div[@class="col-sm-12"]//h4')[-2].text
        data.append(b)
        return data

    def getres_card1(self, driver):
        a = driver.find_elements(By.CLASS_NAME, "help-block")[1].text
        return a

    def review_card(self, data, bank, leixing, num):
        a = data[0]
        b = data[1]
        assert (a == bank + " (" + leixing + ")" and b == "卡号：" + num)

    def review_card1(self, data, expect):
        assert data == expect


class buy_about():

    def find_buy(self, driver):
        driver.find_element(By.XPATH, '//a[@href="javascript:void(0)"]').click()
        driver.find_element(By.PARTIAL_LINK_TEXT, "零钱理财").click()

    def buy_5(self, driver, num, pwd):
        driver.find_elements(By.XPATH, "//tbody//button")[int(num)].click()
        time.sleep(1)
        driver.find_element(By.XPATH, '//div[@class="layui-layer-content"]//input[@id="password"]').send_keys(pwd)
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@class="layui-layer-btn0"]').click()
        time.sleep(1)

    def getres_buy(self, driver):
        res = driver.find_element(By.XPATH, '//div[@class="jconfirm-content"]//div').text
        return res

    def review_buy(self, res, expect):
        assert res == expect


class loan_about():

    def find_loan(self, driver):
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/a').click()
        el = WebDriverWait(driver, 5, 2).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, '安全网贷')))
        el.click()
        time.sleep(1)

    def loan(self, driver, money, shijian):
        a = driver.find_element(By.XPATH, '//input[@id="amount"]')
        a.send_keys(money)
        b = driver.find_element(By.XPATH, '//input[@id="term"]')
        b.send_keys(shijian)
        c = driver.find_element(By.XPATH, '//button[@id="submit"]')
        c.click()
        time.sleep(1)
        driver.find_element(By.XPATH, '//div[@class="jconfirm-buttons"]//button[@class="btn btn-primary"]').click()
        time.sleep(1)

    def getres_loan(self, driver):
        time.sleep(2)
        data = []
        driver.find_element(By.PARTIAL_LINK_TEXT, '个人中心').click()
        time.sleep(2)
        driver.find_element(By.PARTIAL_LINK_TEXT, '我的借贷').click()
        time.sleep(1)
        driver.find_elements(By.XPATH, '//ul[@class="pagination pagination-lg pull-right"]/li/a')[-1].click()
        time.sleep(1)
        a = driver.find_elements(By.XPATH, '//td')[-6].text
        data.append(a)
        b = driver.find_elements(By.XPATH, '//td')[-5].text
        data.append(b)
        return data

    def getres_loan1(self, driver):
        time.sleep(2)
        res = driver.find_element(By.XPATH, '//div[@class="jconfirm-content"]//div').text
        return res

    def review_loan(self, res, money, shijian):
        res_money = res[0]
        res_shijan = res[1]
        money = money + ".00元"
        shijian = shijian + "天"
        assert (res_money == money and res_shijan == shijian)

    def review_loan1(self, res, expect):
        assert res == expect
